package com.puyuba.puyuba.view.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.hyphenate.chat.EMChatRoom;
import com.hyphenate.chat.EMConversation;
import com.hyphenate.chat.EMGroup;
import com.hyphenate.easeui.manager.EaseThreadManager;
import com.hyphenate.easeui.modules.conversation.model.EaseConversationInfo;
import com.puyuba.puyuba.R;
import com.pyb.java.basemodule.BaseFragment;
import com.puyuba.puyuba.utils.custommessage.ConversationListFragment;
import com.pyb.module.ogin.utils.EaseIMUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 消息模块第二个fragment 聊天页面
 */
public class MessageTwoFragment extends BaseFragment {
    private EditText etMessage2;
    private TextView tvSearch;
    private ConversationListFragment chatFragment;
    private List<Object> mData;
    private List<EaseConversationInfo> result=new ArrayList<>();
    @Override
    public void initView() {
        etMessage2 = (EditText) getActivity().findViewById(R.id.et_message2);
        tvSearch = (TextView) getActivity().findViewById(R.id.tv_search_msg2);

        //use EaseChatFratFragment
        chatFragment = getConversationListFragment();
        //pass parameters to chat fragment
        chatFragment.setArguments(getActivity().getIntent().getExtras());
        getActivity().getSupportFragmentManager().beginTransaction().add(R.id.fl_list, chatFragment).commit();

        tvSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                searchResult(etMessage2.getText().toString().trim());
            }
        });
    }

    //搜索消息
    private void searchResult(String search) {
        if(mData == null || mData.isEmpty()) {
            if(mData==null){
                mData=new ArrayList<>() ;

            }
            mData.addAll(chatFragment.conversationListLayout.getListAdapter().mData);
        }
        if(mData == null || mData.isEmpty()) {
            return;
        }
        EaseThreadManager.getInstance().runOnIOThread(()-> {
            result.clear();
            for (Object obj : mData) {
                if(obj instanceof EaseConversationInfo) {
                    EMConversation item = (EMConversation) ((EaseConversationInfo) obj).getInfo();
                    String username = item.conversationId();
                    if(item.getType() == EMConversation.EMConversationType.GroupChat) {
                        EMGroup group = EaseIMUtils.getInstance().getGroupManager().getGroup(username);
                        if(group != null) {
                            if(group.getGroupName().contains(search)) {
                                result.add((EaseConversationInfo)obj);
                            }
                        }else {
                            if(username.contains(search)) {
                                result.add((EaseConversationInfo)obj);
                            }
                        }
                    }else if(item.getType() == EMConversation.EMConversationType.ChatRoom) {
                        EMChatRoom chatRoom = EaseIMUtils.getInstance().getChatroomManager().getChatRoom(username);
                        if(chatRoom != null) {
                            if(chatRoom.getName().contains(search)) {
                                result.add((EaseConversationInfo)obj);
                            }
                        }else {
                            if(username.contains(search)) {
                                result.add((EaseConversationInfo)obj);
                            }
                        }
                    }else {
                        if(username.contains(search)) {
                            result.add((EaseConversationInfo)obj);
                        }
                    }
                }
            }
            getActivity().runOnUiThread(()-> chatFragment.conversationListLayout.getListAdapter().setData(result));
        });
    }

    @NonNull
    private ConversationListFragment getConversationListFragment() {
        ConversationListFragment chatFragment = new ConversationListFragment();
        return chatFragment;
    }

    public static MessageTwoFragment getInstance(String type) {
        MessageTwoFragment messageOneFragment = new MessageTwoFragment();
        Bundle args = new Bundle();
        args.putString("type", type);
        messageOneFragment.setArguments(args);
        return messageOneFragment;
    }
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_message_two;
    }

}
